// 云函数入口文件
const cloud = require('wx-server-sdk')
const rp = require('request-promise')
const TcbRouter = require('tcb-router')
cloud.init()
const URL = 'http://musicapi.xiecheng.live'

// 云函数入口函数
exports.main = async (event, context) => {
  const app = new TcbRouter({ event })

  app.router('toplist',async (ctx) =>{
    let result = await rp(URL + '/toplist/detail')
    result = JSON.parse(result)
    ctx.body = result
  })
  app.router('topDetail',async (ctx) => {
    let idx = parseInt(event.idx)
    let result = await rp(URL + `/top/list?idx=${idx}`)
    result = JSON.parse(result)
    ctx.body = result.playlist.tracks
  })
  

  return app.serve()
}